import Vue from 'vue'
import Router from 'vue-router'
const Home = () => import('@/views/Home.vue')
const Amusement = () => import('@/views/Amusement.vue')
const Login = () => import('@/common/Login.vue')
const compareInfo = () => import('@/common/compareInfo.vue')
const NoFind = () => import('@/common/404.vue')
const Register = () => import('@/common/Register.vue')
const HomeRegister = () => import('@/common/HomeRegister.vue')
const HomeRegisterPuls = () => import('@/common/HomeRegisterPuls.vue')
const CardDetails = () => import('@/views/Home/CardDetails.vue')
const UserInfo = () => import('@/views/UserInfo.vue')
const Modification = () => import('@/views/UserInfo/Modification.vue')
const ModificationAvatar = () =>
	import('@/views/UserInfo/ModificationAvatar.vue')
const ModificatonPassword = () =>
	import('@/views/UserInfo/ModificatonPassword.vue')
/* Admin 后端路由*/
const Admin = () => import('@/views/Admin.vue')
const Dashboard = () => import('@/views/Admin/Dashboard.vue')
const UserList = () => import('@/views/Admin/UserList.vue')
const Role = () => import('@/views/Admin/Role.vue')
const NotFind = () => import('@/views/Admin/404.vue')
Vue.use(Router)

/**
 * vue-router@3.0版本及以上回调形式已经改成promise api的形式了，返回的是一个promise，如果路由地址跳转相同,
 * 且没有捕获到错误，控制台始终会出现如图所示的警告 （注：3.0以下版本则不会出现以下警告！！！，因路由回调问题…）
 *
 */

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch((err) => err)
}

const router = new Router({
	// mode: "history",
	base: process.env.BASE_URL,
	routes: [
		{
			path: '/',
			redirect: '/home',
			component: Home,
		},
		{
			path: '/404',
			name: 'nofind',
			component: NotFind,
		},

		{
			path: '/login',
			name: 'login',
			component: Login,
		},
		{
			path: '/compareInfo',
			name: 'compareinfo',
			component: compareInfo,
		},
		{
			path: '/register',
			name: 'register',
			component: Register,
		},
		{
			path: '/homeRegister',
			name: 'homeregister',
			component: HomeRegister,
		},
		{
			path: '/homeRegisterPuls',
			name: 'homeregisterpuls',
			component: HomeRegisterPuls,
		},
		{
			path: '/CardDetails/:id',
			name: 'carddetails',
			component: CardDetails,
		},
		{
			path: '/home',
			name: 'home',
			component: Home,
		},
		{
			path: '/amusement',
			name: 'amusement',
			component: Amusement,
		},

		{
			path: '/userInfo',
			name: 'userInfo',
			component: UserInfo,
		},
		{
			path: '/modification',
			name: 'modification',
			component: Modification,
		},
		{
			path: '/modificationavatar',
			name: 'modificationavatar',
			component: ModificationAvatar,
		},
		{
			path: '/modificatonPassword',
			name: 'modificatonPassword',
			component: ModificatonPassword,
		},
		{
			path: '/admin',
			name: 'admin',
			component: Admin,
			redirect: '/admin/dashboard',
			meta: {
				title: 'admin',
			},
			children: [
				{
					path: 'dashboard',
					name: 'dashboard',
					component: Dashboard,
					meta: {
						title: '系统首页',
					},
				},
				{
					path: 'userList',
					name: 'userList',
					component: UserList,
					meta: {
						title: '用户信息',
					},
				},
				// {
				//     path: 'role',
				//     name: 'role',
				//     component: Role,
				//     meta: {
				//         title: '用户信息',
				//     },
				// },
				{
					path: 'NotFind',
					name: 'NotFind',
					component: NotFind,
					meta: {
						title: '404页面PC',
					},
				},
				{
					path: 'NoFind',
					name: 'NoFind',
					component: NoFind,
					meta: {
						title: '404页面移动端',
					},
				},
			],
		},
		{
			path: '*', // 此处需特别注意置于最底部
			redirect: '/404',
		},
	],
})

router.beforeEach((to, from, next) => {
	// 登录后可能允许访问
	let refused = [
		'/userInfo',
		'/modificatonPassword',
		'modificationavatar',
		'/admin',
		'/admin/dashboard',
		'/modification',
		'/admin/dashboard',
		'/admin/userList',
	]
	//只有公司管理员登录允许访问
	let refused2 = [
		'/admin',
		'/admin/dashboard',
		'/admin/dashboard',
		'/admin/userList',
	]

	if (refused.includes(to.path)) {
		const tokenStr = window.sessionStorage.getItem('token')
		if (!tokenStr) {
			// 未登录访问权限路径直接跳转到登录页面
			return next('/login')
		}
		if (tokenStr) {
			const admin = JSON.parse(window.sessionStorage.getItem('usersObj'))
			let arr = []
			for (let index = 0; index < admin.roles.length; index++) {
				arr.push(admin.roles[index].code)
			}
			// 公司理员放行所有路径
			if (arr.includes('admin')) {
				return next()
			}
			// 超级管理员不能进入到公司后台
			if (arr.includes('manager')) {
				if (refused2.includes(to.path)) {
					return next('/home')
				} else {
					// 访问后台
					return next()
				}
			}
			// 普通用户不能进入后台
			if (!arr.includes('manager') && !arr.includes('admin')) {
				if (refused2.includes(to.path)) {
					return next('/home')
				} else {
					// 访问后台
					return next()
				}
			}
		}
	}
	next()
})

export default router
